package com.tianci.appstore.ui.gamezone;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.framework.skysdk.ipc.SkyStartActivity;
import com.tianci.appstore.AppDetailActivity;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyAppDetailData;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListStatusData;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;
import com.tianci.appstore.ui.widget.CatchImageView;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;
import com.tianci.appstore.utils.SkyNetImageCache;
import com.tianci.appstore.utils.SkyNetImageCache.ImageLoadListener;

import java.util.HashMap;

public class GameItemView extends LinearLayout implements
        ImageLoadListener, OnClickListener {
    private Context mContext;
    public static int POSTER_WIDTH = SkyConstants.Div(200);
    public static int POSTER_HEIGHT = SkyConstants.Div(300);
    private ImageView posterImage;
    private AlwaysMarqueeTextView titleText;
    protected SkyListAppData data;
    private Bitmap loadedBitmap = null;

    private static final int UPDATE_POSTER = 1;
    protected TextView installedText;
    protected static final int installedTextSize = SkyConstants.Dpi(24);
    protected SkyAppDetailData.AppStatus status;
    FrameLayout posterLayout;

    public GameItemView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.mContext = context;
        this.setOrientation(LinearLayout.VERTICAL);
        this.setClickable(true);
        this.setOnClickListener(this);

        posterLayout = new FrameLayout(mContext);
        posterImage = new CatchImageView(context);
        posterImage.setScaleType(ScaleType.CENTER_INSIDE);
        posterImage.setImageDrawable(SkinManager.getResDrawable(R.drawable.zone_default_poster));
        titleText = new AlwaysMarqueeTextView(context);
        titleText.setTextSize(SkyConstants.Dpi(42));
        titleText.setTextColor(context.getResources().getColor(R.color.titleColor));
        titleText.setPadding(0, SkyConstants.Div(26), 0, SkyConstants.Div(20));
        titleText.setSingleLine();
        titleText.setGravity(Gravity.CENTER);
        posterLayout.addView(posterImage, new LayoutParams(POSTER_WIDTH, POSTER_HEIGHT));
        this.addView(posterLayout, new LayoutParams(POSTER_WIDTH, POSTER_HEIGHT));
        this.addView(titleText, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }

    public void update(SkyAppBaseData data) {
        this.data = (SkyListAppData) data;
        this.loadedBitmap = null;
        titleText.setText(data.getTitle());
        SkyNetImageCache.getSkyNetImageCache().downloadAppListLogo(
                this.data.getPkgName(), this.data.getIconUrl(), this, POSTER_WIDTH,
                POSTER_HEIGHT, 0);
        SkyListStatusData statusData = (SkyListStatusData) data;
        refreshStatus(statusData.getStatus());
    }

    private void refreshStatus(SkyAppDetailData.AppStatus status) {
        this.status = status;
        LogUtils.d("GameItemView", "sunny refreshStatus 33:" + status.toString() + ";" + installedText);
        if (installedText == null) {
            installedText = UICommon.newTextView(mContext, installedTextSize, R.color.defaultColor);
        }
        installedText.setBackgroundResource(R.drawable.poster_installed_bg);
        installedText.setText(R.string.installed);
        installedText.setWidth(SkyConstants.Div(113));
        installedText.setHeight(SkyConstants.Div(48));
        installedText.setGravity(Gravity.CENTER);
        if (installedText.getParent() == null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SkyConstants.Div(113), SkyConstants.Div(48), Gravity.BOTTOM | Gravity.RIGHT);
            posterLayout.addView(installedText, params);
        }

        if (status == SkyAppDetailData.AppStatus.INSTALLED || status == SkyAppDetailData.AppStatus.INSTALLED_WITH_CAN_NOT_BE_UNINSTALLED) {
            if (installedText != null)
                installedText.setVisibility(View.VISIBLE);
        } else {
            if (installedText != null && installedText.getParent() != null) {
                if (installedText != null)
                    installedText.setVisibility(View.GONE);
            }
        }
    }

    public void updateAppStatus(SkyAppDetailData.AppStatus status) {
        refreshStatus(status);
    }


    private void updatePoster() {
        if (loadedBitmap == null)
            return;
        posterImage.setScaleType(ScaleType.FIT_XY);
        posterImage.setImageBitmap(loadedBitmap);
    }

    public void setSelected(boolean selected) {
        titleText.setSelected(selected);
    }

    @Override
    public void loadFinished(String url, Bitmap result) {
        // TODO Auto-generated method stub
        if (url == null || result == null || url.equals("") || !url.equals(data.getIconUrl()))
            return;
        LogUtils.d("zone", "ZoneItem load bitmap finished, url = " + url);
        this.loadedBitmap = result;
        Message msg = mainHandler.obtainMessage(UPDATE_POSTER);
        msg.obj = this;
        mainHandler.sendMessage(msg);
    }

    private static Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case UPDATE_POSTER:
                    GameItemView itemView = (GameItemView) msg.obj;
                    itemView.updatePoster();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (!isNetConnected())
            return;

        if (data == null) {
            LogUtils.d("game_zone", "Game zone item clicked, but data = null !!!!!!!!!!!!");
            return;
        }
        String strId = data.getId();
        if (strId == null || strId.trim().length() == 0) {
            LogUtils.d("zone", "strId is error!!!");
            return;
        }
        LogUtils.d("zone", "strId :" + this.data.getId());
        Intent intent = new Intent(mContext, AppDetailActivity.class);
        intent.putExtra("id", this.data.getId());
//        mContext.startActivity(intent);
        HashMap<String,String> map = new HashMap<String,String>();
        SkyStartActivity.startActivity(mContext, mContext.getClass().getName(), intent, map, false);
    }

    protected boolean isNetConnected() {
        return LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable);
    }
}
